/**
 * 异步初始化树
 * 带选项框
 * @param {Object} ztreeId --节点id
 * @param {Object} dataUrl -- 请求地址
 * @param {Object} type -- 请求类型get|post
 * @param {Object} enableChkBox -- 是否显示选择框
 */
function asyncInitZTreeObject(ztreeId, dataUrl, type, enableChkBox){
	// 获取异步加载设置对象
	var setting;
	if(enableChkBox){
		setting = getAsyncChkBoxSetting(dataUrl, type);
	}else{
		setting = getAsyncNoChkBoxSetting(dataUrl, type);
	}
	$.fn.zTree.init($("#" + ztreeId), setting);
}

/**
 * 使用本地数据初始化对象
 * @param {Object} ztreeId -- 节点id
 * @param {Object} nodeList -- 数据集合
 * @param {Object} enableChkBox -- 是否开启选择框
 */
function initZTreeObjectByData(ztreeId, nodeList, enableChkBox){
	// 获取异步加载设置对象
	var setting;
	if(enableChkBox){
		setting = getAsyncChkBoxSetting("", "");
	}else{
		setting = getAsyncNoChkBoxSetting("", "");
	}
	$.fn.zTree.init($("#" + ztreeId), setting, nodeList);
}

/**
 * 销毁数据
 * @param {Object} ztreeId
 */
function destroyZTree(ztreeId){
	var ztreeObj = $.fn.zTree.getZTreeObj(ztreeId);
	if(ztreeObj){
		ztreeObj.destroy();
	}
}

/**
 * 使用异步加载数据方式创建
 * 不带选择框的设置对象
 * @param {Object} dataUrl
 * @param {Object} type
 */
function getAsyncNoChkBoxSetting(dataUrl, type){
	var setting = getAsyncChkBoxSetting(dataUrl, type);
	setting.check["enable"] = false;
	return setting;
}

/**
 * 使用异步加载数据方式创建带选择框的设置对象
 * @param {Object} dataUrl
 * @param {Object} type
 */
function getAsyncChkBoxSetting(dataUrl, type){
	var asyncFalg = false;
	if(dataUrl != null && "" != dataUrl && dataUrl != undefined){
		asyncFalg = true;
	}
	var setting = {
		async: {
			enable: asyncFalg,
			type: type,
			url: dataUrl
		},
		data: {
			keep: {
				leaf: false,
				parent: false
			},
			key: {
				checked: "checked",
				children: "children",
				name: "name",
				title: "",
				url: "url"
			},
			simpleData: {
				enable: false,
				idKey: "id",
				pIdKey: "pid",
				rootPid: null
			}
		},
		check: {
			autoCheckTrigger: false,
			chkboxType: {
				"Y": "ps",
				"N": "ps"
			},
			chkStyle: "checkbox",
			enable: true,
			nocheckInherit: false,
			chkDisabledInherit: false,
			radioType: "level"
		},
		view: {
			showIcon: false
		}
	};
	return setting;
}

